﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Caching;
using System.Web;
using System.Collections;

namespace CachedServices
{
    public class DefaultCacheProvider : ICacheProvider
    {
        private System.Web.Caching.Cache Cache { get { return HttpRuntime.Cache; } }      

        public object Get(string key)
        {
            return Cache[key];
        }

        public void Set(string key, object data, int cacheTime)
        {
            Cache.Add(key, data, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(cacheTime), System.Web.Caching.CacheItemPriority.AboveNormal, null);
        }

        public bool IsSet(string key)
        {
            return (Cache[key] != null);
        }

        public void Invalidate(string key)
        {
            Cache.Remove(key);
        }

        public void CrearCache()
        {
            foreach (DictionaryEntry entry in Cache)
            {
                Cache.Remove(entry.Key.ToString());
            }
        }
    }
}
